package com.scalar.chiptrack.operations.dataentry.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.utils.SqlQueryMappings;

public class TestDataEntryDAO{

	
	public TestDataEntryDAO(){
	}

	
	

public ArrayList getBinNumber(int processStepid) throws ScalarException  {


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
				 ArrayList binList = new ArrayList();
				 BinQuantity binQuantity = null;

				 
                		
		try{
				
		
        conn = DataSource.getConnection();                    
		pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.test.binNo"));
		pstmt.setInt(1,processStepid);
        rs = pstmt.executeQuery();

		

		while(rs.next()){
        binQuantity = new BinQuantity();
		binQuantity.setBinNumber(rs.getString(1));
		//System.out.println("Good bin Number "+rs.getString(1));
		binList.add(binQuantity);
		}
			
        
	}
			catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not get Bin Numbers from database", sqle);
        }
				
			finally{
					if (pstmt != null) {
						try { pstmt.close(); } catch (SQLException ignore){}
					}
					if (conn != null) {
						try { conn.close(); } catch (SQLException ignore){}
					}
					}
			
			return binList;
		
        }
		


		public ArrayList getBinQuantities(String lotId,ArrayList binList) throws ScalarException{


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
				 BinQuantity binQuantity = null;
				 ArrayList binQuantityList = null;
				 
                		
		try{
				
		
        conn = DataSource.getConnection();
		binQuantityList = new ArrayList();

		for(int i=0;i<binList.size();i++){
        binQuantity = (BinQuantity) binList.get(i);
	    pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.test.binQuantities"));
		pstmt.setString(1,lotId);
		pstmt.setString(2,binQuantity.getBinNumber());
		rs = pstmt.executeQuery();
	
		if(rs.next()){
        binQuantity.setBinQuantity(rs.getString(1));
		binQuantityList.add(binQuantity);
		}
		pstmt.close();
		}

        
	}
			catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not getBinQuantities from database", sqle);
        }
			finally{

				 if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore){}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore){}
            }
			}
			
			return binQuantityList;
		
        }	


	}

	























	
	
